-- UIBuildingView3
-- Created by chengb Apr/14/2015
-- 建筑查看界面（包含额外描述，目前仅用于新手关卡）

UIBuildingView3 = class("UIBuildingView3", function()
    return cc.Layer:create();
end);

function UIBuildingView3.create(gridData, viewFunc, iconOffsetY, dialog, viewText, title, iconPath, disabled, hideBack)
    return UIBuildingView3.new(gridData, viewFunc, iconOffsetY, dialog, viewText, title, iconPath, disabled, hideBack);
end

function UIBuildingView3:ctor(gridData, viewFunc, iconOffsetY, dialog, viewText, title, iconPath, disabled, hideBack)
    -- 初始化
    self:setName("UIBuildingView3");
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView3.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class) or {};

    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end

    self.title     = title or buildingInfo["title"];                            -- 标题
    self.iconPath  = iconPath or getLevelItemIconPath(buildingInfo["icon"]);    -- 图标路径
    self.dialog    = dialog or BuildingBonusM.getBuildingDialog(self.gridData); -- 对话内容
    self.okBtnText = viewText or buildingInfo["view_text"];                     -- 确定按钮文本
    self.iconOffsetY = iconOffsetY or cfgOffsetY;                               -- 图标纵向偏移量
    self.viewFunc  = viewFunc;                                                  -- 回调函数
    self.disabled = disabled or false;                                          -- 按钮是否置灰
    self.hideBack = hideBack or false;                                          -- 是否隐藏返回按钮
    self.extraDesc = buildingInfo["extra_desc"] or "";                          -- 额外描述

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBuildingView3:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBuildingView3:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBuildingView3 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBuildingView3:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView3");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            if type(self.viewFunc) == 'function' then
                self.viewFunc();
            end

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView3");
        end
    end
    AddTouchEventListener(btnOk, onOkClick);
end

-- 重绘
function UIBuildingView3:redraw()
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    local titleNode  = findChildByName(self.node, "CT/title_node");
    local titleLabel = findChildByName(titleNode, "title");
    titleLabel:setString(self.title);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    local iconImg = findChildByName(self.node, "CT/icon");
    iconImg:loadTexture(self.iconPath);
    iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);

    local iconSize = iconImg:getContentSize();
    if self.gridData.type == GRID_TYPE_TRAP then
        local trapId = self.gridData.id;
        local trapType = CommonTrapM.query(trapId, "type");

        -- 如果是猎人陷阱，还要绘制弓箭
        if trapType == TRAP_TYPE_BEELINE then
            local arowImg = ccui.ImageView:create("images/ui/level/item/arow.png");
            local bowImg  = ccui.ImageView:create("images/ui/level/item/bow.png");
            iconImg:addChild(bowImg);
            iconImg:addChild(arowImg);
            arowImg:setPosition(iconSize.width/2, iconSize.height/2);
            bowImg:setPosition(iconSize.width/2, iconSize.height/2);

            -- 旋转箭头方向
            local direction =  self.gridData.direction;

            if direction == "up" then
            -- 上，不需要旋转
            elseif direction == "right" then
                -- 右
                arowImg:setRotation(90);
                bowImg:setRotation(90);
            elseif direction == "down" then
                -- 下
                arowImg:setRotation(180);
                bowImg:setRotation(180);
            elseif direction == "left" then
                -- 左
                arowImg:setRotation(270);
                bowImg:setRotation(270);
            end
        end
    end

    iconImg:setScale(1.3);

    -- 设置对话内容
    local descLabel  = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    descLabel = applyString(descLabel, self.dialog);

    -- 设置额外描述
    local desc2Label = findChildByName(self.node, "CT/desc2");
    if self.extraDesc == "" then
        desc2Label:setVisible(false);
    else
        desc2Label:setVisible(true);
        TextStyleM.setTextStyle(desc2Label, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
        desc2Label = applyString(desc2Label, self.extraDesc);
    end

    -- 设置确定按钮状态
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(self.okBtnText);
    if self.disabled then
        btnOk:setTouchEnabled(false);
        btnOk:setBright(false);
    else
        btnOk:setTouchEnabled(true);
        btnOk:setBright(true);
    end

    -- 确认按钮如果为"离开"，则返回按钮不显示
    if self.okBtnText == getLocStr("btn_text_leave") then
        self.hideBack = true;
    end

    -- 设置返回按钮显隐
    local btnBack = findChildByName(self.node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
    if self.hideBack then
        btnBack:setVisible(false);
    else
        btnBack:setVisible(true);
    end
end










